package com.jbl.ma.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jbl.commonutils.R;
import com.jbl.ma.entity.UserApply;
import com.jbl.ma.entity.UserApplyEvidence;
import com.jbl.ma.mapper.UserApplyMapper;
import com.jbl.ma.service.UserApplyEvidenceService;
import com.jbl.ma.service.UserApplyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jbl.ma.vo.UserApplyQuery;
import com.jbl.ma.vo.UserApplyVo;
import com.jbl.te.entity.School;
import com.jbl.te.entity.Teacher;
import com.jbl.te.entity.User;
import com.jbl.te.service.SchoolService;
import com.jbl.te.service.TeacherService;
import com.jbl.te.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author jbl
 * @since 2020-12-11
 */
@Service
public class UserApplyServiceImpl extends ServiceImpl<UserApplyMapper, UserApply> implements UserApplyService {

    @Autowired
    private SchoolService schoolService;
    @Autowired
    private UserApplyEvidenceService userApplyEvidenceService;
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private UserService userService;

    @Override
    public List<UserApplyVo> pageQuery(Page<UserApply> pageParam, UserApplyQuery userApplyQuery) {
        QueryWrapper<UserApply> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("gmt_modified");

        if (userApplyQuery == null) {
            this.page(pageParam, wrapper);
            return null;
        }
        Integer isVerified = userApplyQuery.getIsVerified();
        String name = userApplyQuery.getName();


        if (!StringUtils.isEmpty(isVerified)) {
            wrapper.eq("is_verified", isVerified);
        }
        if (!StringUtils.isEmpty(name)) {
            QueryWrapper<Teacher> teacherQueryWrapper = new QueryWrapper<>();
            teacherQueryWrapper.select("id");
            teacherQueryWrapper.likeRight("name",name);
            List<Teacher> teacherList = teacherService.list(teacherQueryWrapper);
            ArrayList<String> idList = new ArrayList<>();
            for (Teacher teacher : teacherList) {
                idList.add(teacher.getId());
            }
            if(!idList.isEmpty()) wrapper.in("teacher_number",idList);
        }

        this.page(pageParam, wrapper);

        List<UserApply> records = pageParam.getRecords();

        List<UserApplyVo> collect = getUserApplyVos(records);

        return collect;
    }

    @Override
    public List<UserApplyVo> getApplyList() {
        List<UserApply> dataList = this.list();

        List<UserApplyVo> collect = getUserApplyVos(dataList);

        return collect;
    }

    private List<UserApplyVo> getUserApplyVos(List<UserApply> dataList) {
        return dataList.stream().map(userApply -> {
            UserApplyVo userApplyVo = new UserApplyVo();
            BeanUtils.copyProperties(userApply, userApplyVo);
            UserApplyEvidence evidence = userApplyEvidenceService.getById(userApply.getApplyId());
            userApplyVo.setContent(evidence.getContent());
            userApplyVo.setRole(evidence.getRole());

            Teacher teacher = teacherService.getById(userApply.getTeacherNumber());
            userApplyVo.setTeacherName(teacher.getName());
            userApplyVo.setPhone(teacher.getPhone());
            userApplyVo.setPhoto(teacher.getPhoto());
            userApplyVo.setLicense(teacher.getLicense());

            return userApplyVo;
        }).collect(Collectors.toList());
    }

    @Override
    public UserApplyVo isVerified(String teacherNumber) {
        UserApply apply = this.getOne(
                new QueryWrapper<UserApply>()
                        .eq("teacher_number", teacherNumber)
        );
        UserApplyEvidence evidence = userApplyEvidenceService.getById(apply.getApplyId());
        UserApplyVo userApplyVo = new UserApplyVo();
        BeanUtils.copyProperties(apply, userApplyVo);
        BeanUtils.copyProperties(evidence, userApplyVo);
        return userApplyVo;
    }

    @Transactional
    @Override
    public boolean pass(String applyId) {
        if (applyId != null) {
            // 传来applyId 修改 isVerified==1
            UserApply userApply = new UserApply();
            userApply.setIsVerified(1);
            boolean update = this.update(userApply,
                    new UpdateWrapper<UserApply>().eq("apply_id", applyId));
            //修改角色权限
            if(update) {
                UserApply apply = this.getById(applyId);
                User user = userService.getById(apply.getTeacherNumber());
                if(user.getRole().equals("common_teacher")) {
                    Teacher teacher = teacherService.getById(user.getId());
                    teacher.setIsVerified(1);
                    teacherService.updateById(teacher);
                }

                UserApplyEvidence evidence = userApplyEvidenceService.getById(applyId);
                user.setRole(evidence.getRole());
                boolean update1 = userService.updateById(user);
                return update1;
            }
            return update;
        } else {
            return false;
        }
    }

    @Transactional
    @Override
    public boolean passBatch(List<String> applyIds) {
        // 传来applyId 修改 isVerified==1
        // System.out.println("applyids: " + applyIds);
        if (applyIds != null) {
            for (String applyId : applyIds) {
                boolean pass = pass(applyId);
            }
            return true;
        } else {
            return false;
        }
    }
}
